'use strict';

const Service = require('egg').Service;

class FavapiService extends Service {
  /**
   * 根据用户名和apiId查找数据
   * @param {string} username 用户名
   * @param {number} apiId apiId
   */
  async findOneFavApi(username, apiId) {
    const { mysql } = this.app
    const result = await mysql.select('fav', {
      where: {
        username,
        api_id: apiId
      }
    })
    return result
  }

  /**
   * 收藏api
   * @param {string} username 用户名
   * @param {number}} apiId apiId
   */
  async favPai (username, apiId, projectId) {
    const { mysql } = this.app
    const result = await mysql.insert('fav', {
      username,
      api_id: apiId,
      project_id: projectId
    })
    return result
  }

  /**
   * 删除收藏
   * @param {number} id fav表id
   */
  async deleteFav (id) {
    const { mysql } = this.app
    const result = await mysql.delete('fav', {
      id
    })
    return result
  }

  /**
   * 根据用户名查询接口
   * @param {string} username 用户名
   */
  async findApiByUsername (username) {
    const { mysql } = this.app
    const result = await mysql.select('fav', {
      where: {
        username
      }
    })
    return result
  }
}

module.exports = FavapiService;
